/**
 * 无跳转，直接POST通讯方式的接口
 *
 * @author xiezz
 * @version 1.1.2
 */
package com.pay.sdk.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.pay.sdk.comm.IRequestService;
import com.pay.sdk.common.Configure;
import com.pay.sdk.util.HttpTookit;
import com.pay.sdk.util.JsonUtils;
import com.pay.sdk.util.MD5Utils;


public abstract class IPostService {

    /**
     * 通讯接口，返回报文为json格式
     *
     * @param params post待发送的包含所有参数的Map
     * @return JSON报文
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws MalformedURLException
     * @throws KeyManagementException
     */
    protected static String txn(String url, Map<String,String> params) {
    	   String jsonStr = null;
           try {
               IRequestService requestor = IRequestService.getInstance();
               jsonStr =  (String) requestor.sendPost(url, params);
               if (Configure.isNeedChkSign()) {
   				Map<String,String> signMap = JsonUtils.jsonToMap(jsonStr);
   				if (signMap.containsKey("sign")&& !MD5Utils.verify(signMap)) {
   					jsonStr = Configure.SIGN_ERROR_RESULT;
   				}
   			}
           } catch (Exception e) {
           	jsonStr = Configure.SYS_ERROR_RESULT;
           }
           return jsonStr;
    }

    /**
     * 外层调用接口，只传入需要用户输入的参数，并补全所有参数
     *
     * @param params
     * @return JSON报文
     */
    public abstract String exec(Map<String, String> params);
}
